# -*- coding:utf-8 -*-
"""
日内高低点突破
"""
from function.interface import *


class Strategy5(JyInterface):
    symbol = 'BTC/USDT'
    period = '1min'
    takeprofit = 0.02
    stoplosses = 0.01
    amount = 0.01
    lastTradePrice = 0
    def __init__(self, **kwargs):
        super(Strategy5, self).__init__(**kwargs)
        self.klinePeriod = self.period
        self.period = '1s'
        
    def loop(self):
        """策略部分"""
        self.period = '1s'
        kline = self.getKline(self.symbol, self.klinePeriod, 2)
        hlimit = max(kline['closes'][-2],kline['opens'][-2])
        llimit = min(kline['closes'][-2],kline['opens'][-2])
        price = self.getLastPrice(self.symbol)
        position = self.getPositionSt(self.symbol)
        if position > 0 and (price >= self.lastTradePrice*(1+self.takeprofit) or price <= self.lastTradePrice*(1-self.stoplosses)):
            self.sell(self.symbol, price, position)
            self.period = self.klinePeriod
        if position == 0 and price >= hlimit:
            self.buy(self.symbol, price, self.amount)
            self.lastTradePrice = price
            
        if self.futureOrSpot == 1 and position < 0 and (price>=self.lastTradePrice*(1+self.stoplosses) or price<=self.lastTradePrice*(1-self.takeprofit)):
            self.cover(self.symbol, price, -position)
            self.period = self.klinePeriod
        if self.futureOrSpot == 1 and position == 0 and price <= llimit:
            self.short(self.symbol, price, self.amount)
            self.lastTradePrice = price


if __name__ == "__main__":
    st = Strategy5(type_=1,futureOrSpot=0,appKey='e11650-78937',secret='bf36b9-c08f5',passphrase='',transferId=101)
    st.run(st.loop)